package com.itboye.ihomebank.home;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.HuXingAdapter;
import com.itboye.ihomebank.adapter.SouSuoAdapter;
import com.itboye.ihomebank.adapter.TeSeAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.HouseListBean;
import com.itboye.ihomebank.bean.HouseListBeanCopy;
import com.itboye.ihomebank.bean.HouseListBeanCopy.Acreage;
import com.itboye.ihomebank.bean.HouseListBeanCopy.Floor;
import com.itboye.ihomebank.bean.HouseListBeanCopy.HouseItem;
import com.itboye.ihomebank.bean.HouseListBeanCopy.HouseTag;
import com.itboye.ihomebank.bean.HouseListBeanCopy.HouseType;
import com.itboye.ihomebank.bean.HouseListBeanCopy.Order;
import com.itboye.ihomebank.bean.HouseListBeanCopy.Orientation;
import com.itboye.ihomebank.bean.HouseListBeanCopy.RentType;
import com.itboye.ihomebank.bean.HouseListBeanCopy.Rental;
import com.itboye.ihomebank.bean.HouseListBeanCopy.Source;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SouSuoBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.FlowLayout;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.db.DBManager;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 首页的搜索
 * 
 */
public class ActivityHotSouSuo extends BaseOtherActivity implements Observer {
	private ImageView sousuo_finish;// 返回
	private EditText sousuo_txt;;// 输入搜索内容
	private ImageView sousuo_dingwei;;// 定位
	private LinearLayout sousuo_weizhi, sousuo_zujin, sousuo_paixu,
			sousuo_gengduo;

	private XListView sousuo_listview;
	LinearLayout main;
	SouSuoAdapter adapter;
	private List<SouSuoBean> arrayLists = new ArrayList<>();
	HousePresenter housePresenter;
	PopupWindow popupWindow;
	private SQLiteDatabase database;

	private String cityCode;
	private String city;
	private DBManager dbManager;
	private LinearLayout layout;
	private ListView rootList;
	private FrameLayout flChild;
	private ListView childList;
	private PopupWindow mPopWin;
	List<HouseListBeanCopy.HouseItem> arrayArea = new ArrayList<HouseListBeanCopy.HouseItem>();
	List<HouseListBeanCopy.HouseItem.InnerItem> innerItemsArray = new ArrayList<HouseListBeanCopy.HouseItem.InnerItem>();
	List<HouseItem> area = new ArrayList<HouseListBeanCopy.HouseItem>();
	List<Rental> rental = new ArrayList<HouseListBeanCopy.Rental>();
	List<Acreage> acreage = new ArrayList<HouseListBeanCopy.Acreage>();
	List<HouseType> houseType = new ArrayList<HouseListBeanCopy.HouseType>();
	List<RentType> rentType = new ArrayList<HouseListBeanCopy.RentType>();
	List<Orientation> orientation = new ArrayList<HouseListBeanCopy.Orientation>();
	List<Floor> floor = new ArrayList<HouseListBeanCopy.Floor>();
	List<Source> source = new ArrayList<HouseListBeanCopy.Source>();
	List<HouseTag> houseTag = new ArrayList<HouseListBeanCopy.HouseTag>();
	List<Order> order = new ArrayList<HouseListBeanCopy.Order>();
	private GridView gridView;
	private GridView guigeContainer2;
	private HuXingAdapter huXingAdapter;
	private TeSeAdapter teseAdapter;
	TextView no_data;
	/**
	 * cityCode 城市code searchText 查询文本 areaOrZone 为空不限,区或区域 rental 为空不限,租金
	 * 自定义查询规则 brp:大于等于多少 erp:小于等于多少 例如 brp100erp200 大于等于100小于等于200
	 * aceage为空不限,面积 rba:大于等于多少 rea:小于等于多少 例如 rba100rea200 大于等于100小于等于200
	 * houseType为空不限,房型 rentType 租房类型 decoration 为空不限,装修 oritation 为空不限,朝向 floor
	 * 为空不限,楼层 source 为空不限,房源 用来查找（个人，经纪人） houseTag order 为空不限,排序 perPage
	 * 每页条数，默认10条一页 currentPage 页码，默认第一页
	 */
	protected String areaOrZone = "";
	protected String searchText = "";
	protected String aceage = "";
	protected String rentalMoney = "";
	protected String house_tag = "";
	protected int pageNo = 1;
	protected String orderType = "";
	protected String house_type = "";
	LinearLayout top;
	FlowLayout tag_container;

	@Override
	protected int layout_id() {
		return R.layout.activity_home_sousuo;
	}

	@Override
	protected void onInit() {
		showProgressDialog("获取热门搜索词中", false);
		housePresenter = new HousePresenter(this);
		sousuo_dingwei.setVisibility(View.GONE);
		top.setVisibility(View.GONE);
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		dbManager.closeDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);
		city = SPUtils.get(this, null, SPContants.SELECT_CITY, "") + "";
		if (city != null) {
			cityCode = getCityCode(city);
		}
		if (cityCode != null) {
			getHotTag(cityCode);
		}
		sousuo_txt
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (sousuo_txt.getText().toString().equals("")) {
							ByAlert.alert("请输入搜索关键词");
							return false;
						}
						if (actionId == EditorInfo.IME_ACTION_SEARCH
								|| (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
							Intent intent = new Intent(ActivityHotSouSuo.this,
									ActivityHomeSouSuo.class);
							intent.putExtra("hot", sousuo_txt.getText()
									.toString());
							startActivity(intent);
							return true;
						}
						return false;
					}
				});
	}

	private void getHotTag(String cityCode) {
		// TODO Auto-generated method stub
		housePresenter.getHotTag(cityCode);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.sousuo_finish:
			finish();
			break;
		case R.id.sousuo_txt:// 输入内容

			break;
		default:
			break;
		}

	}

	private String getCityCode(String city) {
		// TODO Auto-generated method stub
		Cursor cursor = null;
		if (database != null)
			cursor = database.query("hat_city", new String[] { "cityID" },
					"city=?", new String[] { city.equals("") ? "杭州市" : city },
					null, null, null);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				cityCode = cursor.getString(0);
			}
			cursor.close();
		}
		if (database != null) {
			database.close();
		}
		Log.v("cityId", cityCode + "---------------" + city);
		return cityCode;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	private HouseListBean houseListBean;

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.hotTag_success) {
				List<String> listStr = (List<String>) entity.getData();
				if (listStr != null) {

					tag_container.setVisibility(View.VISIBLE);
					for (int i = 0; i < listStr.size(); i++) {
						View view = LayoutInflater.from(ActivityHotSouSuo.this)
								.inflate(R.layout.item_biaoqian, null);
						TextView textView = (TextView) view
								.findViewById(R.id.btn_name);
						textView.setBackgroundResource(R.drawable.shape_biaoqian);
						android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams(
								android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
								android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
						textView.setTag(i);
						// // view.setClickable(true);
						view.setTag(listStr.get(i));
						textView.setText(listStr.get(i));
						layoutParams.setMargins(5, 5, 5, 5);
						tag_container.addView(view, layoutParams);
						textView.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								ByAlert.alert("11111");
								ByAlert.alert("dianjile"
										+ v.getTag().toString());
								Intent intent = new Intent(
										ActivityHotSouSuo.this,
										ActivityHomeSouSuo.class);
								intent.putExtra("hot", v.getTag().toString());
								startActivity(intent);
							}
						});

					}
					if (listStr.size() <= 0) {
						no_data.setVisibility(View.VISIBLE);
						tag_container.setVisibility(View.GONE);
					}
				} else {
					no_data.setVisibility(View.VISIBLE);
					tag_container.setVisibility(View.GONE);
				}

			} else {
				ByAlert.alert(entity.getData());
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
